//
//  JSPhotoChooseViewController.m
//  JigSaw
//
//  Created by Slim Wang on 15/1/17.
//  Copyright (c) 2015年 LITB. All rights reserved.
//

#import "JSPhotoChooseViewController.h"
#import "JSPhotoChooseCell.h"
#import "JSDeviceInfo.h"
#import "JSPreviewManager.h"
#import "JSModelEditViewController.h"
#import "UIColor+Help.h"
#import "PGJSImageItem.h"
#define BASE_CELL_WIDTH 79
#define BASE_CELL_HEIGHT 79
#define BASE_CELL_MIN_SPACE 1
#define BASE_LINE_MIN_SPACE 1


@interface JSPhotoChooseViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, ImageAddPreViewDelegate>

@property(nonatomic, assign)float adaptScale;
@property(nonatomic, weak)IBOutlet UICollectionView* collectionView;

@end

@implementation JSPhotoChooseViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHex:0xe8e8e8];
    self.collectionView.backgroundColor = [UIColor colorWithHex:0xe8e8e8];
    self.adaptScale = [JSDeviceInfo getScreenScale];
    if (!self.assets) {
        self.assets = [@[] mutableCopy];
    }
    self.type = E_ALBUM_SYS;
    self.title = NSLocalizedString(@"选择照片", @"");
    
    //为了进入相册后显示到最底部
    NSInteger rows = self.assets.count / 4 + 5;
    CGFloat cellHeight = BASE_CELL_HEIGHT*self.adaptScale;
    self.collectionView.contentSize = CGSizeMake(self.view.frame.size.width, cellHeight*rows);
    self.collectionView.contentOffset = CGPointMake(0, cellHeight*rows);
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[JSPreviewManager sharedInstance] showPreView];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [[JSPreviewManager sharedInstance] preview].delegateSelectImage = self;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[JSPreviewManager sharedInstance] preview].delegateSelectImage = nil;
}

//系统相册通过group获取所有assets, C360相册可以直接设置assets
- (void)setGroup:(ALAssetsGroup *)group
{
    _group = group;
    NSMutableArray* tempArray = [@[] mutableCopy];
    
    [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
        if (result) {
            [tempArray addObject:result];
        }
    }];
    self.assets = tempArray;
}

- (void)setAssets:(NSMutableArray *)assets
{
    _assets = assets;
    [self.collectionView reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.assets.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    JSPhotoChooseCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"photoCell" forIndexPath:indexPath];
    id asset = [self.assets objectAtIndex:indexPath.row];
    if ([asset isKindOfClass:[ALAsset class]]) {
        cell.photoIV.image = [UIImage imageWithCGImage:((ALAsset*)asset).thumbnail];
    } else if ([asset isKindOfClass:[PGJSImageItem class]]) {
        //TODO C360的相册数据类型处理
        cell.photoIV.image = ((PGJSImageItem*)asset).thumbnail;
    }
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(BASE_CELL_WIDTH*self.adaptScale, BASE_CELL_HEIGHT
                      *self.adaptScale);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return BASE_LINE_MIN_SPACE*self.adaptScale;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return BASE_CELL_MIN_SPACE*self.adaptScale;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    id asset = [self.assets objectAtIndex:indexPath.row];
    if ([asset isKindOfClass:[ALAsset class]]) {
        [[[JSPreviewManager sharedInstance] preview] addImageWith:asset];
    } else if ([asset isKindOfClass:[PGJSImageItem class]]) {
        [[[JSPreviewManager sharedInstance] preview] addImageWithImageItem:asset];
    }
}

- (void)startPintuAction:(JSImageAddPreView *)sender
{
    NSArray* imageAssets = ((JSImageAddPreView *)sender).imageassets;
    if (imageAssets.count > 0) {
        [self performSegueWithIdentifier:@"showModelEdit1" sender:imageAssets];
    } else {
        //TODO alert 请选择图片
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"showModelEdit1"]) {
        NSArray* imageAssets = sender;
        JSModelEditViewController* controller = segue.destinationViewController;
        controller.imageAssets = imageAssets;
    }
}

@end
